package com.xiaoshuidi.cloud.module.system.service.receipt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.system.controller.admin.apartment.vo.ApartmentListReqVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.apartment.vo.ApartmentRespVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.receipt.vo.ReceiptReqVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.receipt.vo.ReceiptRespVO;
import com.xiaoshuidi.cloud.module.system.convert.apartment.ApartmentConvert;
import com.xiaoshuidi.cloud.module.system.convert.receipt.ReceiptConvert;
import com.xiaoshuidi.cloud.module.system.dal.dataobject.apartment.ApartmentDO;
import com.xiaoshuidi.cloud.module.system.dal.dataobject.receipt.ReceiptDO;
import com.xiaoshuidi.cloud.module.system.dal.mysql.apartment.ApartmentMapper;
import com.xiaoshuidi.cloud.module.system.dal.mysql.receipt.ReceiptMapper;
import com.xiaoshuidi.cloud.module.system.enums.apartment.ApartmentManageEnum;
import com.xiaoshuidi.cloud.module.system.service.apartment.ApartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 门店管理 Service 实现类
 *
 * @author
 */
@Service
@Validated
@Slf4j
public class ReceiptServiceImpl implements ReceiptService {


    @Resource
    private ReceiptMapper receiptMapper;
    @Resource
    private ApartmentService apartmentService;

    /**
     * 根据门店id获取收据配置
     *
     * @param apartmentId
     * @return
     */
    @Override
    public ReceiptRespVO get(Long apartmentId) {
        ReceiptDO receiptDO = receiptMapper.selectOne(ReceiptDO::getApartmentId, apartmentId);
        return ReceiptConvert.INSTANCE.convert(receiptDO);
    }

    /**
     * 保存配置
     *
     * @param reqVO
     * @return
     */
    @Override
    public Boolean save(ReceiptReqVO reqVO) {
        //先通过apartmentId查询是否存在，不存在先初始化
        Long count = receiptMapper.selectCount(ReceiptDO::getApartmentId, reqVO.getApartmentId());
        if (count > 0){
            //存在就做更新操作
            LambdaUpdateWrapper<ReceiptDO> uw = new LambdaUpdateWrapper<>();
            uw.eq(ReceiptDO::getApartmentId, reqVO.getApartmentId())
                    .set(ReceiptDO::getIsEnabled,reqVO.getIsEnabled())
                    .set(ReceiptDO::getFinancialSealUrl,reqVO.getFinancialSealUrl());
            receiptMapper.update(null,uw);
            return true;
        }
        //不存在就初始化一条
        ReceiptDO receiptDO = ReceiptConvert.INSTANCE.convert1(reqVO);
        receiptMapper.insert(receiptDO);
        //初始化时更新关机配置信息
        apartmentService.updateApartment(reqVO.getApartmentId(),ApartmentManageEnum.RECEIPT_CONFIG.getCode());
        return true;
    }
}
